package com.yashi.springmvcjbpm.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.jbpm.api.ProcessDefinition;
import org.jbpm.api.ProcessInstance;
import org.jbpm.api.task.Task;
import com.yashi.springmvcjbpm.dao.impl.LeaveDAOImpl;
import com.yashi.springmvcjbpm.dao.impl.StaffDAOImpl;
import com.yashi.springmvcjbpm.entity.Leave;
import com.yashi.springmvcjbpm.util.JBPMUtil;



public class JBPMServiceImpl  {

	private JBPMUtil jBPMUtil = new JBPMUtil();
	private LeaveDAOImpl leaveDAO = new LeaveDAOImpl();
	private StaffDAOImpl staffDAOImpl = new StaffDAOImpl();
	private LeaveDAOImpl leaveDAOImpl = new LeaveDAOImpl();

	// 发布流程
	public String deployNew(String resourceName, String resourceimgname) {
		return jBPMUtil.deployNew(resourceName,resourceimgname);
	}

	// 发布流程
	public String deployZipNew(String resourceZipName) {
		return jBPMUtil.deployZipNew(resourceZipName);
	}

	// 开始流程实例
	public ProcessInstance startPI(String staffName, String id) {
		Map map = new HashMap();
		map.put("staff", staffName);
		return jBPMUtil.startPIById(id, map);

	}

	// 完成任务
	public void completeTask(String taskId) {

		jBPMUtil.completeTask(taskId);
	}

	// 完成任务
	public void completeTask(String taskId, String result) {
		Map map = new HashMap();
		map.put("result", result);
		jBPMUtil.completeTask(taskId, result);
	}

	// 添加请假信息进入库表
	public void applyLeave(String staffName, String leaveLong, String leaveContent, String leaveInstanceId) {		
		String queryString = "select * from Staff where staffName='" + staffName + "'";
//		leaveLong = RuleServiceimpl.changeVale(leaveLong);
		List list = staffDAOImpl.findStaff(queryString);
		Map staffMap = (Map) list.get(0);
		System.out.println("name="+(String)staffMap.get("staffName"));
		Leave leave = new Leave();
		leave.setLeaveContent(leaveContent);
		leave.setLeaveLong(leaveLong);
		leave.setStaffName((String)staffMap.get("staffName"));
		leave.setLeaveState("审核中");
		leave.setLeaveInstanceId(leaveInstanceId);
		leaveDAOImpl.saveLeave(leave);
	}

	// 更新请假信息的流程状
	public void updateLeave(Map leave, String result) {
		System.out.println("map="+leave.get("leaveId"));
		String staffName = (String)leave.get("staffName");
		StaffDAOImpl staffDaoImpl = new StaffDAOImpl();
		String staffPosition = staffDaoImpl.getStaffPosition(staffName);
		System.out.println("staffPosition="+staffPosition+",result="+result);
		String updateSql = null;
		if (leave.get("leaveState").equals("审核中") && staffPosition.equals("staff") && result.equals("批准")) {
			System.out.println("map="+leave.get("leaveState"));
			 updateSql ="update `leave` set leaveState='经理审核通过' where leaveId="+leave.get("leaveId");
		} else if (leave.get("leaveState").equals("经理审核通过") && result.equals("批准")) {
			updateSql ="update `leave` set leaveState='老板审核通过' where leaveId="+leave.get("leaveId");
		} else if (leave.get("leaveState").equals("审核中") && staffPosition.equals("manager") && result.equals("批准")) {
			updateSql ="update `leave` set leaveState='老板审核通过' where leaveId="+leave.get("leaveId");
		}
		System.out.println("updateSql="+updateSql);
		leaveDAO.updateLeave(updateSql);
	}

	// 被驳回的请假信息重新填写
	public void updateLeave(Map leave, String leaveLong, String leaveContent) {
//		leaveLong = RuleServiceimpl.changeVale(leaveLong);
		String updateSql ="update `leave` set leaveLong='"+leaveLong+"', leaveContent='"+leaveContent+"' where leaveId="+leave.get("leaveId");
		System.out.println(updateSql);
		leaveDAO.updateLeave(updateSql);
	}

	public String getTaskId(String staffName) {
		return jBPMUtil.findPersonalTasks(staffName).get(0).getId();
	}

	public List<Task> getTasksList(String assignee) {
		System.out.println("assignee=" + assignee);

		return jBPMUtil.findPersonalTasks(assignee);
	}

	public List<Leave> getLeaveDetail(String taskId) {
		System.out.println("taskId="+taskId);
		String leaveInstanceId = jBPMUtil.getTaskService().getTask(taskId).getExecutionId();
		System.out.println("leaveInstanceId=" + leaveInstanceId);
		String queryString = "select * from `Leave` where leaveInstanceId='" + leaveInstanceId + "'";
		return leaveDAO.findLeaves(queryString);
	}

	public List<ProcessDefinition> getAllPd() {
		return jBPMUtil.getAllPdList();
	}
	public List<ProcessInstance> getAllPiList() {
		return jBPMUtil.getAllPiList();
	}

	public void completeTask(String taskId,String leaveLong, String leaveContent) {
		Map map = new HashMap();
		map.put("leaveLong", leaveLong);
		map.put("leaveContent", leaveContent);
		jBPMUtil.completeTask(taskId,map);
	}

}
